$(document).ready(function(){
    
    var parent_cid = 0; 
    var cur_page=0;
    var parseQueryString = function( queryString ) {
        var params = {}, queries, temp, i, l;
     
        // Split into key/value pairs
        queries = queryString.split("&");
     
        // Convert the array of strings into an object
        for ( i = 0, l = queries.length; i < l; i++ ) {
            temp = queries[i].split('=');
            params[temp[0]] = temp[1];
        }
     
        return params;
    };

    $(".message-wrapper .addcommentform input[type='submit']").live('click', function()
    {
        if(parent_cid>0)
            var comments_list_wrapper = $(".comments-list-wrapper").html();
        else
            var container = $(".comments-container").html();    
        
        $(".addcommentform").ajaxSubmit( {
            dataType: "json",
            url: "ajax_respondent.php"+(parent_cid>0?'/?page='+cur_page:''),
            beforeSubmit: function()
            {
                if(parent_cid>0)
                    $(".comments-list-wrapper").html("<div class=\"text-center spinner\"><img src=\"img/spinner.gif\"></div>");
                else
                    $(".comments-container").html("<div class=\"text-center spinner\"><img src=\"img/spinner.gif\"></div>");
            },
            success: function(data) {
                    
                if(data.error==1)
                {
                    if(parent_cid>0)
                        $(".comments-list-wrapper").html(comments_list_wrapper);
                    else
                        $(".comments-container").html(container);    
                }
                else
                {
                    if(parent_cid>0)
                        $(".comments-list-wrapper").html(data.html);
                    else
                        $(".comments-container").html(data.html);
                }
                $('.cancel').live('click',function()
                {
                    $('a.link_reply').show();
                    $(".addcommentroot").show();
                    $(".cancel").parents('.message-wrapper').first().remove();
                    return false;
                });
                $(".addcommentroot").show();
                    
                $('.alert-wrapper').html(data.messages_html);
            //alert("Спасибо за комментарий!");
            }
        });
    return false;
    });





$(".addcommentroot").live('click',function()
{
    //$('textarea').wysihtml5();
    // $(".pagination li:last").first().trigger('click');
    $('.alert-wrapper').html("");
    parent_cid = 0;
    var comments_wrapper = $(".comments-list-wrapper");
    $(".addcommentroot").hide()
    $('a.link_reply').hide();
    $(this).hide();
    $.ajax({
        async:false,
        url:"ajax_respondent.php",
        dataType:"json",
        type:"GET",
        beforeSend:function(){
            comments_wrapper.append("<div class=\"text-center spinner\"><img src=\"img/spinner.gif\"></div>");
        },
        success:function(data){  
            comments_wrapper.find(".spinner").remove();
            var comment_block = $(data.html);
            comment_block.find('.control-group').last().append($("<a class=\"cancel\" href=\"#\">отмена</a>").click(function()
            {
                $('a.link_reply').show();
                $(".addcommentroot").show();
                comment_block.remove();
                return false;
            }));
            comments_wrapper.append(comment_block);
                    
        },
        error:function(){
        },
        complete:function(){
        },
        data: {
            action: "ajax_getaddcommentform",
            parent: 0
        }
    });
    return false;
});
    
$("a.link_reply").live('click',function()
{   
    $('.alert-wrapper').html("");
    that = this;
    var current_comment_block = $(this).parents('.message-wrapper').first();
    var current_message_inner_wrapper = current_comment_block.find(".message-inner-wrapper").first();
    //$(".addcommentform").parents(".message").first().hide();
    $(".addcommentroot").hide();
         
    $('a.link_reply').hide();
        
    var parent_id = parseQueryString(that.href)['cid']; 
    parent_cid = parent_id;
    cur_page  = parseQueryString(that.href)['page']; 
    $.ajax({
        async:false,
        url:"ajax_respondent.php",
        dataType:"json",
        type:"GET",
        beforeSend:function(){
            current_message_inner_wrapper.append("<div class=\"text-center spinner\"><img src=\"img/spinner.gif\"></div>");
        },
        success:function(data){
                    
            current_message_inner_wrapper.find(".spinner").remove();
            var comment_block = $(data.html);
            comment_block.find('.control-group').last().append($("<a class=\"cancel\" href=\"#\">отмена</a>").click(function()
            {
                $('a.link_reply').show();
                $(".addcommentroot").show();
                comment_block.remove();
                return false;
            }));
            current_message_inner_wrapper.append(comment_block);
                
                
        },
        error:function(){
        },
        complete:function(){
        },
        data: {
            action: "ajax_getaddcommentform",
            parent: parent_id
                
        }
    });
        
        
    return false;
});
$(".pagination li").live('click',function()
{
    var cur_page = parseQueryString($(this).find('a').attr('href'))['?page'];
    var comments_wrapper = $(".comments-container");
        
    $.ajax({
        async:false,
        url:"ajax_respondent.php",
        dataType:"json",
        type:"GET",
        beforeSend:function(){
            comments_wrapper.html("<div class=\"text-center spinner\"><img src=\"img/spinner.gif\"></div>");
        },
        success:function(data){
            comments_wrapper.html(data.html);
        },
        error:function(){
        },
        complete:function(){
        },
        data: {
            action: "ajax_getpage",
            page:(cur_page)
                
        }
    });
    return false;
});

});